package com.seele.tigerwallet.ui.manager;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.DatabaseConnection;
import com.seele.tigerwallet.model.AssetsModel;
import com.seele.tigerwallet.model.ContactsModel;
import com.seele.tigerwallet.model.MarketModel;
import com.seele.tigerwallet.model.MessageModel;
import com.seele.tigerwallet.model.TokenInfoModel;
import com.seele.tigerwallet.model.TsRecord;
import com.seele.tigerwallet.ui.orm.DatabaseHelper;
import com.seele.tigerwallet.model.TsModel;

import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.List;

public class DaoManager {

    public synchronized static <T> int saveOrUpdate(T t) {
        if (t == null) {
            return 0;
        }
        try {
            Dao<T, Integer> dao = DatabaseHelper.getInstance().getDao(t.getClass());
            if (null != dao) {
                return dao.createOrUpdate(t).getNumLinesChanged();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * @return 查询所有数据
     */
    public static <T> List<T> queryAll(T t) {
        try {
            Dao<T, Integer> dao = DatabaseHelper.getInstance().getDao(t.getClass());
            return dao.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static Dao getMarketDao() {
        try {
            return DatabaseHelper.getInstance().getDao(MarketModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Dao getAssetsDao() {
        try {
            return DatabaseHelper.getInstance().getDao(AssetsModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Dao getTokenDao() {
        try {
            return DatabaseHelper.getInstance().getDao(TokenInfoModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Dao getContactsDao() {
        try {
            return DatabaseHelper.getInstance().getDao(ContactsModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Dao getTsModelsDao() {
        try {
            return DatabaseHelper.getInstance().getDao(TsModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Dao getTsRecordDao() {
        try {
            return DatabaseHelper.getInstance().getDao(TsRecord.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Dao getMessageModelsDao() {
        try {
            return DatabaseHelper.getInstance().getDao(MessageModel.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
